﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports Boogaloo.Core.UI
Imports Boogaloo.Core.BLL

Namespace UI.UserControls

    Partial Public Class Site_TreeScroller
        Inherits AdminNavControlBase

        Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init

            If current_node Is Nothing Then
                div_add_node.Visible = False
                div_breadcrumb.Visible = False
            End If


        End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            hyp_cancel_add_node.NavigateUrl = Request.Url.PathAndQuery

            If current_node IsNot Nothing AndAlso _
                Not current_node.layout Then



                Dim bcCached As List(Of Node) = Node.GetNodeBreadCrumb(current_node.node_id)

                ' clone the list so that modifications do not affected the cached version in case
                ' used somewhere else in the application
                breadcrumb = New List(Of Node)
                Dim bc As Node = Nothing
                For Each bc In bcCached
                    breadcrumb.Add(bc)
                Next


                If breadcrumb.Count < 3 Then
                    Do Until breadcrumb.Count = 3
                        breadcrumb.Add(Nothing)
                    Loop
                End If
                rpt_breadcrumb.DataSource = breadcrumb
                rpt_breadcrumb.DataBind()

                'Dim expand_breadcrumb As List(Of Node) = Nothing
                If GetEventTargetClicked().StartsWith(rpt_tree.ClientID) Then
                    Dim new_breadcrumb_id As String = GetEventTargetClicked().Substring(GetEventTargetClicked().IndexOf("lbexpand") + 8)
                    expand_breadcrumb = Node.GetNodeBreadCrumb(CType(new_breadcrumb_id, Integer))
                Else
                    expand_breadcrumb = breadcrumb
                End If


                Dim branchPages As New List(Of Node)
                Dim bp As Node = Nothing
                For Each bp In expand_breadcrumb
                    If bp IsNot Nothing Then branchPages.Add(bp)
                Next
                rpt_tree.DataSource = branchPages
                rpt_tree.DataBind()

                rpt_branchpages.DataSource = branchPages
                rpt_branchpages.DataBind()

                '<asp:hyperlink id="hyp_branchpages" runat="server" text="Settings for this list" />

            End If

            If Not Page.IsPostBack Then
                BindControl()
            End If

        End Sub

        Private Sub BindControl()


            lb_addref_sibling.Enabled = False
            div_add_node.Visible = False
 

        End Sub


#Region "BreadCrumb"

        Protected Sub rpt_breadcrumb_ItemDatabound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_breadcrumb.ItemDataBound

            Dim n As Node = CType(e.Item.DataItem, Node)
            Dim hyp_node As HyperLink = CType(e.Item.FindControl("hyp_node"), HyperLink)
            Dim lb_open_nodeform As LinkButton = CType(e.Item.FindControl("lb_open_nodeform"), LinkButton)
            Dim spn_nopages As HtmlGenericControl = CType(e.Item.FindControl("spn_nopages"), HtmlGenericControl)

            If n IsNot Nothing Then

                hyp_node.NavigateUrl = GetGoToNodeUrl(n.node_id)
                If n.menu_item_text.Length > 18 Then
                    hyp_node.Text = String.Format("{0}...", n.menu_item_text.Substring(0, 15))
                Else
                    hyp_node.Text = n.menu_item_text
                End If

                hyp_node.ToolTip = n.page_link_text

                lb_open_nodeform.CommandArgument = n.node_id
                spn_nopages.Visible = False

            Else
                If hyp_node IsNot Nothing Then hyp_node.Visible = False
                If lb_open_nodeform IsNot Nothing Then lb_open_nodeform.Visible = False
            End If


        End Sub

        Protected Sub rpt_branchpages_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_branchpages.ItemDataBound

            Dim n As Node = CType(e.Item.DataItem, Node)
            If n IsNot Nothing Then

                Dim hyp As HyperLink = CType(e.Item.FindControl("hyp"), HyperLink)
                hyp.NavigateUrl = String.Format("{0}BranchPages.aspx?node={1}", Groove.path_admin_site, n.parent_id)

            End If

        End Sub

        Protected Sub rpt_tree_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_tree.ItemDataBound

            Dim n As Node = CType(e.Item.DataItem, Node)
            If n IsNot Nothing Then

                Dim tree As Site_TreeScrollerChildTree = CType(Me.LoadControl(String.Format("{0}Site_TreeScrollerChildTree.ascx", Groove.path_admin_controls)), Site_TreeScrollerChildTree)
                tree.ref_node = expand_breadcrumb(e.Item.ItemIndex)
                tree.breadcrumb = expand_breadcrumb
                tree.ID = "tree"

                Dim ph As PlaceHolder = CType(e.Item.FindControl("ph_tree"), PlaceHolder)
                ph.Controls.Add(tree)

            End If

        End Sub

#End Region

#Region "Linkbuttons"

        Protected Sub lb_open_nodeform_Click(ByVal sender As Object, ByVal e As EventArgs)

            div_add_node.Visible = True

            Dim node_id As Integer = CType(CType(sender, LinkButton).CommandArgument, Integer)
            Dim ref As Node = Node.GetNode(node_id)

            lb_addref_sibling.Text = String.Format(lb_addref_sibling.Text, ref.menu_item_text)
            lb_addref_subpage.Text = String.Format(lb_addref_subpage.Text, ref.menu_item_text)


            lb_add_node.CommandArgument = CType(sender, LinkButton).CommandArgument

        End Sub


        Protected Sub lb_addref_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_addref_subpage.Click, lb_addref_sibling.Click

            If lb_addref_sibling.Enabled Then
                lb_addref_sibling.Enabled = False
                lb_addref_subpage.Enabled = True
            Else
                lb_addref_sibling.Enabled = True
                lb_addref_subpage.Enabled = False
            End If

        End Sub

        Protected Sub lb_add_node_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_add_node.Click

            Dim node_id As Integer = 0

            If Page.IsValid Then
                Dim lb As LinkButton = CType(sender, LinkButton)
                Dim ref_node As Node = Node.GetNode(CType(lb.CommandArgument, Integer))

                Dim parent As Node = Nothing
                If Not lb_addref_sibling.Enabled Then
                    'parent = Node.GetNode(current_node.parent_id)
                    parent = ref_node.parent_node
                Else
                    'parent = current_node
                    parent = ref_node
                End If


                If parent IsNot Nothing Then
                    node_id = parent.SpawnChildNode(txt_menu_item_text.Text)
                Else ' this is a root level node (special case)
                    node_id = Node.InsertRootLevelNode(Groove.admin_root.root_id, txt_menu_item_text.Text)
                End If

            End If
            If node_id = 0 Then
                Groove.RefreshPage()
            Else
                Response.Redirect(String.Format("{0}Designer.aspx?node={1}", Groove.path_admin_site, node_id.ToString))
            End If

        End Sub

#End Region




        Public readonly Property current_node() As Node
            Get
                If Groove.admin_current_node Is Nothing Then
                    If Groove.admin_root.default_node Is Nothing Then
                        Return Nothing
                    Else
                        Return Groove.admin_root.default_node
                    End If
                Else
                    If Groove.admin_current_node.layout Then
                        Return Groove.admin_root.default_node
                    Else
                        Return Groove.admin_current_node
                    End If
                End If
            End Get
        End Property


        Private _expand_breadcrumb As List(Of Node)
        Public Property expand_breadcrumb() As List(Of Node)
            Get
                Return _expand_breadcrumb
            End Get
            Set(ByVal value As List(Of Node))
                _expand_breadcrumb = value
            End Set
        End Property


    End Class

End Namespace